package com.example.readcard;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**") // 允许所有 API 端点
                        .allowedOrigins(
                                "http://localhost:8080",
                                "http://localhost:8081",
                                "http://localhost:8082",
                                "http://localhost:8084",
                                "http://localhost:8087",
                                "https://www.relukj.cn:443",
                                "http://localhost:5174",
                                "http://localhost:8083",
                                "https://www.relukj.cn"
                        ) // 允许的前端地址
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的方法
                        .allowedHeaders("*") // 允许的请求头
                        .allowCredentials(true); // 允许 Cookie
            }
        };
    }
}
